package team.fivez.defaultmanage.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import team.fivez.defaultmanage.dao.*;
import team.fivez.defaultmanage.domain.Customer;
import team.fivez.defaultmanage.domain.DefaultApplication;
import team.fivez.defaultmanage.domain.RedoApplication;
import team.fivez.defaultmanage.domain.RedoReason;
import team.fivez.defaultmanage.mediumDomain.CompletedRedoApplication;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

@Component
public class RedoApplicationService {

    private final RedoApplicationDao redoApplicationDao;

    private final CustomerDao customerDao;

    private final RedoReasonDao redoReasonDao;

    private final DefaultApplicationDao defaultApplicationDao;

    private final ApproverDao approverDao;

    @Autowired
    public RedoApplicationService(RedoApplicationDao redoApplicationDao, CustomerDao customerDao, RedoReasonDao redoReasonDao, DefaultApplicationDao defaultApplicationDao, ApproverDao approverDao) {
        this.redoApplicationDao = redoApplicationDao;
        this.customerDao = customerDao;
        this.redoReasonDao = redoReasonDao;
        this.defaultApplicationDao = defaultApplicationDao;
        this.approverDao = approverDao;
    }




    //新增重生申请，0成功，1失败
    public int addNewRedoApplication(String customerName,int redoNumber,String applicantAccount) throws InterruptedException {

        //确认该客户是不是存在
        Integer count = customerDao.selectCountByName(customerName);
        if(count!=1){
            return 1;
        }
        //确认该客户是否已经在违约状态
        Customer customerFromDB = customerDao.getCustomerByName(customerName);
        if(customerFromDB.getDefaultStatus()!=1){
            return 1;
        }

        Integer countByName = redoApplicationDao.countByName(customerName);
        if (countByName==1){
            return 1;
        }

        //申请单信息完备
        DefaultApplication defaultApplication = defaultApplicationDao.getDefaultApplicationByCustomerName(customerName);

        if (defaultApplication.getApprovementStatus()!=1){
            return 1;
        }

        String applicationNumber="RAN"+System.currentTimeMillis();//生成违约单号
        Thread.sleep(2);
        int i = redoApplicationDao.insertRedoApplication(
                new RedoApplication(applicationNumber,
                        defaultApplication.getSeverity(),
                        new Timestamp(System.currentTimeMillis()),
                        applicantAccount,
                        defaultApplication.getCustomerName(),
                        redoNumber));

        if(i==1) return 0;
        else return 1;
    }

    //申请人的重生清单页面的初始化
    public List<CompletedRedoApplication> listSelectedCustomerByApplicant(String applicantAccount){

        List<RedoApplication> listByApplicantAccount = redoApplicationDao.getListByApplicantAccount(applicantAccount);

        return completeRedoApplicationList(listByApplicantAccount);
    }

    //申请人根据客户名进行查找
    public List<CompletedRedoApplication> listSelectedCustomerByApplicantAndCustomerName(String applicantAccount,String customerName){
        List<RedoApplication> listByName = redoApplicationDao.getListByCustomerNameAndApplicantAccount(customerName, applicantAccount);

        return completeRedoApplicationList(listByName);
    }

    //申请人根据审核状态进行查找
    public List<CompletedRedoApplication> listSelectedCustomerByApplicantAndStatus(String applicantAccount,int status){
        List<RedoApplication> listByStatus = redoApplicationDao.getListByApprovementStatusAndApplicantAccount(status, applicantAccount);
        return completeRedoApplicationList(listByStatus);
    }

    //审核人初始化列表
    public List<CompletedRedoApplication> listApplication(){
        List<RedoApplication> allList = redoApplicationDao.getAllList();
        return completeRedoApplicationList(allList);
    }

    //审核人按照客户名称查询
    public List<CompletedRedoApplication> listApplicationByCustomerName(String customerName){
        List<RedoApplication> listByCustomerName = redoApplicationDao.getListByCustomerName(customerName);
        return completeRedoApplicationList(listByCustomerName);
    }

    //审核人按照审核状态进行查询
    public List<CompletedRedoApplication> listApplicationByStatus(int status){
        List<RedoApplication> listByApprovementStatus = redoApplicationDao.getListByApprovementStatus(status);

        return completeRedoApplicationList(listByApprovementStatus);
    }




    //完善所有的重生申请表中的信息
    public List<CompletedRedoApplication> completeRedoApplicationList(List<RedoApplication> list){
        List<CompletedRedoApplication> completedList = new ArrayList<>();
        for (RedoApplication redoApplication : list) {

            //获取原因
            RedoReason redoReason = redoReasonDao.getRedoReasonByNumber(redoApplication.getRedoNumber());

            //获取客户信息
            String customerName = redoApplication.getCustomerName();
            Customer customerInfo = customerDao.getCustomerByName(customerName);

            completedList.add(new CompletedRedoApplication(customerInfo,redoApplication,redoReason.getRedoReason()));

        }


        return completedList;
    }

    //更新审核状态
    public int updateStatus(int status,String applicationNumber,String approverAccount){
        RedoApplication redoApplication = redoApplicationDao.getRedoApplicationByNumber(applicationNumber);
        if (redoApplication.getApprovementStatus()!=0){
            return 1;//失败
        }
        if(status==2){
            redoApplicationDao.updateApprovementStatusByApplicationNumber(status,applicationNumber);
            //更新审核人姓名
            redoApplicationDao.updateDefaultApproverNameByApplicationNumber(applicationNumber,
                    approverDao.getApproverByApproverAccount(approverAccount).getApproverName());

            //更新审核时间
            redoApplicationDao.updateApplicationTimeByApplicationNumber(applicationNumber,
                    new Timestamp(System.currentTimeMillis()));

        } else if (status==1) {//同意就是将用户改为未违约的状态

            redoApplicationDao.updateApprovementStatusByApplicationNumber(status,applicationNumber);
            customerDao.updateStatusByCustomerName(redoApplication.getCustomerName(),0);
            //更新审核人姓名
            redoApplicationDao.updateDefaultApproverNameByApplicationNumber(applicationNumber,
                    approverDao.getApproverByApproverAccount(approverAccount).getApproverName());

            //更新审核时间
            redoApplicationDao.updateApplicationTimeByApplicationNumber(applicationNumber,
                    new Timestamp(System.currentTimeMillis()));

            String number = defaultApplicationDao.getDefaultApplicationByCustomerName(redoApplicationDao.getRedoApplicationByNumber(applicationNumber).getCustomerName()).getApplicationNumber();

            //更新重生状态
            defaultApplicationDao.updateRedoStatusByApplicationNumber(number,1);
        }

        return 0;//成功
    }



}
